from unit_management.models import Quantity

# 根据quantity获取它可用的单位


def get_units_by_quantity(quantity_entity):
    unit_list = []
    quantity_unit_map_list = quantity_entity.quantity_unit_map_quantity.all()
    for quantity_unit_map in quantity_unit_map_list:
        if quantity_unit_map.unit.valid_flag:
            unit_list.append(quantity_unit_map.unit)
    return unit_list

# 根据单位集获取它的单位内容


def get_unit_set_by_unitset(unitset_entity):
    unitset_choice = {}
    unitset_content = unitset_entity.unitsetcontent_set.all()
    for content in unitset_content:
        if content.quantity.valid_flag:
            unitset_choice[str(content.quantity.uuid)] = [
                str(content.unit.uuid)]
    return unitset_choice

# 根据单位集获取它的单位内容-文字版本


def get_unit_set_by_unitset_text(unitset_entity):
    unitset_choice = {}
    unitset_content = unitset_entity.unitsetcontent_set.all()
    for content in unitset_content:
        if content.quantity.valid_flag:
            unitset_choice[str(content.quantity.uuid)] = "{}({})".format(
                content.unit.show_short_name(), content.unit.unit_symbol)
    return unitset_choice
